"""
@name : 04.装饰器的应用
@author : wenyao
@projectname: xsfh
"""
import logging
logger=logging.getLogger()
fh=logging.FileHandler("sc.log",encoding='utf-8')
ch=logging.StreamHandler()
foematter=logging.Formatter("%(asctime)s-%(levelname)s:%(message)s")
fh.setFormatter(foematter)
ch.setFormatter(foematter)
logger.addHandler(fh)
logger.addHandler(ch)
logger.setLevel(logging.DEBUG)

def Name(func):
    print("i am name")
    def inner(*args,**kwargs):
        re=func(*args,**kwargs)
        logger.warning(f"执行了{func.__name__}函数")
        return re
    return inner

import time
import functools

def Time(func):
    print("i am time")
    #  保留传递进来的函数的元数据，将它的元数据赋值给inner
    # 保留元数据
    @functools.wraps(func)
    def inner(*args,**kwargs):
        start=time.time()
        re=func(*args,**kwargs)
        end=time.time()
        print(f"共花费了{end-start}")
        return re
    return inner

# @Time     #func1 = Time(Name(func1))
@Name       #func1  = Name(Time(func1))
@Time
def func1(a,b):
    time.sleep(2)
    return a

func1(a=1,b=2)
func1(1,2)

#针对装饰器而言封装从上到下， 执行从下到上
# @Name  # func2 = Name(func2)
# def func2():
#     print(" i am func2")
#
# func2()
# print(func1("af"))
# # 从下到上依次执行
#
# func2()